define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'qrcode', 'layer', 'upload'], function ($, undefined, Backend, Table, Form, qrcode, layer, Upload) {
 
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                queryParams:function(params) {
                    // 要显示的字段
                    // params.fields = '';
                    // // 要关联的拓展信息
                    // params.extra = 'partner,open_auth';
                    return params;
                },
                // search:true,
                // commonSearch: true,
                // searchFormVisible: false,
                columns: [
                    [
                        {field: 'id', title: __('ID'),operate:false},
                        {field: 'order_no', title: '订单号',align:'left'},
                        {field: 'goods_title', title: '商品标题',align:'left'},
                        {field: 'goods_type_name', title: '商品类型'},
                        {field: 'nickname', title: '用户昵称',operate: false},
                        {field: 'number', title: '兑换数量',operate: false},
                        {field: 'realname', title: '联系人',operate: false},
                        {field: 'stage_name', title: '阶段'},
                        {field: 'points', title: '消费积分',operate: false},
                        {field: 'status_name', title: '状态'},
                        {field: 'create_time', title: '创建时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit:function () {
            Controller.api.bindevent();
        },
        delivery:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                operate: function (value, row, index) {
                    var html = [];
                    switch (row.stage) {
                        case 0 :
                            html.push('<a href="javascript:;" class="btn btn-success btn-confrim btn-xs"><i class="fa fa-pencil"></i> 确认</a>');
                            break;
                        case 1 :
                            html.push('<a href="javascript:;" class="btn btn-success btn-delivery btn-xs"><i class="fa fa-pencil"></i> 发货</a>');
                            break;
                        case 2 :
                            html.push('<a href="javascript:;" class="btn btn-success btn-finish btn-xs"><i class="fa fa-pencil"></i> 完成</a>');
                            break;
                    }
                    if(row.status == 0) {
                        html.push('<a href="javascript:;" class="btn btn-danger btn-cancel btn-xs"><i class="fa fa-pencil"></i> 取消</a>');
                    }
                    html.push('<a href="javascript:;" class="btn btn-info btn-view btn-xs"><i class="fa fa-pencil"></i> 详情</a>');
                    return html.join(' ');
                }
            },
            events:{
                operate: $.extend({
                    // 确认
                    'click .btn-confrim': function (e, value, row, index) {
                        e.stopPropagation();
                        var table = $(this).closest('table');
                        if(confirm("确认此操作")) {
                            Fast.api.ajax({
                                url : "/goods/admin.order/confrim",
                                data : { order_no : row.order_no}
                            }, function (res) {
                                Toastr.success(__('Operation completed'));
                                table.bootstrapTable('refresh');
                            });
                        }
                    },

                    // 发货
                    'click .btn-delivery': function (e, value, row, index) {
                        e.stopPropagation();
                        Fast.api.open('/goods/admin.order/delivery?order_no=' + row.order_no, '发货');
                    },


                    // 取消
                    'click .btn-cancel': function (e, value, row, index) {
                        e.stopPropagation();
                        var table = $(this).closest('table');
                        if(confirm("确认此操作")) {
                            Fast.api.ajax({
                                url : "/goods/admin.order/cancel",
                                data : { order_no : row.order_no}
                            }, function (res) {
                                Toastr.success(__('Operation completed'));
                                table.bootstrapTable('refresh');
                            });
                        }
                    },

                    // 详情
                    'click .btn-view': function (e, value, row, index) {
                        e.stopPropagation();
                        Fast.api.open('/goods/admin.order/edit?ids=' + row.id, __('Edit'));
                    },


                    // 完成
                    'click .btn-finish': function (e, value, row, index) {
                        e.stopPropagation();
                        var table = $(this).closest('table');
                        if(confirm("确认此操作")) {
                            Fast.api.ajax({
                                url : "/goods/admin.order/finish",
                                data : { order_no : row.order_no}
                            }, function (res) {
                                Toastr.success(__('Operation completed'));
                                table.bootstrapTable('refresh');
                            });
                        }
                    }


                }, Table.api.events.operate)
            }
        }
    }
    return Controller;
});